package org.setaria.control
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayList;
	import mx.events.FlexMouseEvent;
	
	import org.setaria.event.PageSearchEvent;
	import org.setaria.skins.control.PageControlSkin;
	import org.setaria.vo.Page;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.NumericStepper;
	import spark.components.PopUpAnchor;
	
	[Event(name="pageSearch",type="org.setaria.event.PageSearchEvent")]
	
	public class PageControl extends BorderContainer
	{
		public function PageControl()
		{
			super();
			this.setStyle("skinClass",PageControlSkin)
		}
		/**
		 * 分页信息
		 * */
		private var _page:Page = new Page();
		
		public function set page(page:Page):void{
			_page.index = page.index;
			_page.data = page.data;
			_page.size = page.size;
			_page.sum = page.sum;
			_page.total = page.total;
			pageSizeNs.value = _page.size;
			goNs.maximum = _page.total;
			setControlButtonEnabled();
			createPageLabelList();
		}
		public function get page():Page{
			return _page;
		}
		/**
		 * 页码列表长度
		 * */
		public var pageLabelSize:int = 9;
		
		
		private var pageLbls:ArrayList = new ArrayList();
		
		//////////////////////////////////////////
		///三个显示信息的label
		///////////////////////////////////////////
		[SkinPart(required="false")]
		/**
		 * 显示当前页和总页数
		 * */
		public var currentPageLbl:Label;
		[SkinPart(required="false")]
		/**
		 * 显示和设置 每页显示数量
		 * */
		public var pageSizeLbl:Label;
		[SkinPart(required="false")]
		/**
		 * 显示总数据量
		 * */
		public var sumLbl:Label;
		
		////////////
		///设置每页显示数据量
		/////////////
		[SkinPart(required="false")]
		public var pua:PopUpAnchor;
		[SkinPart(required="false")]
		public var pageSizeNs:NumericStepper;
		
		/////////////////////////////////
		////控制显示当前页的4个按钮
		////////////////////////////////
		[SkinPart(required="false")]
		public var firstPageBtn:Button;
		[SkinPart(required="false")]
		public var prePageBtn:Button;
		[SkinPart(required="false")]
		public var nextPageBtn:Button;
		[SkinPart(required="false")]
		public var lastPageBtn:Button;
		
		[SkinPart(required="false")]
		/**
		 * 页码列表的容器
		 * */
		public var indexGroup:Group;
		[SkinPart(required="false")]
		/**
		 * 页码跳转设置
		 * */
		public var goNs:NumericStepper;
		
		[SkinPart(required="false")]
		/**
		 * 根据页码跳转查询
		 * */
		public var goBtn:Button;
		[SkinPart(required="false")]
		/**
		 * 控制是否显示页码列表
		 * */
		public var controlBtn:Button;
		
		
		
		/**
		 *  @private
		 */
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			if(instance == currentPageLbl){
				BindingUtils.bindProperty(currentPageLbl,"text",_page,{ name:"index", getter:function pageinfo(p:Page):String
				{return "第"+p["index"]+"/"+p["total"]+"页";}});
				BindingUtils.bindProperty(currentPageLbl,"text",_page,{ name:"total", getter:function pageinfo(p:Page):String
				{return "第"+p["index"]+"/"+p["total"]+"页";}});
			}else if(instance == pageSizeLbl){
				BindingUtils.bindProperty(pageSizeLbl,"text",_page,{ name:"size", getter:function sizeinfo(p:Page):String
				{return "每页"+p.size+"条";}});
			}else if(instance == sumLbl){
				BindingUtils.bindProperty(sumLbl,"text",_page,{ name:"sum", getter:function suminfo(p:Page):String
				{return "总计"+p.sum+"条";}});
			}else if(instance == pua){

			}else if(instance == pageSizeNs){
				pageSizeNs.addEventListener(KeyboardEvent.KEY_DOWN,keyboardSizeSearch);
				pageSizeNs.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE,closePua);
			
			}else if(instance == firstPageBtn){
				firstPageBtn.addEventListener(MouseEvent.CLICK,firstPage);
			}else if(instance == prePageBtn){
				prePageBtn.addEventListener(MouseEvent.CLICK,prePage);
			}else if(instance == nextPageBtn){
				nextPageBtn.addEventListener(MouseEvent.CLICK,nextPage);
			
			}else if(instance == lastPageBtn){
				lastPageBtn.addEventListener(MouseEvent.CLICK,lastPage);
			}else if(instance == indexGroup){
				createPageLabelList();
			}else if(instance == goNs){
				goNs.addEventListener(KeyboardEvent.KEY_DOWN,keyboardPageSearch);
				goNs.maximum = _page.total;
			}else if(instance == goBtn){
				goBtn.addEventListener(MouseEvent.CLICK,goBtnHandler);
			}else if(instance == controlBtn){
				
			}
		}
		/**
		 *  @private
		 */
		override protected function partRemoved(partName:String, instance:Object):void
		{
			if (instance == pageSizeNs){
				pageSizeNs.removeEventListener(KeyboardEvent.KEY_DOWN,keyboardSizeSearch);
				pageSizeNs.removeEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE,closePua);
			}else if(instance == firstPageBtn){
				firstPageBtn.removeEventListener(MouseEvent.CLICK,firstPage);
			}else if(instance == prePageBtn){
				prePageBtn.removeEventListener(MouseEvent.CLICK,prePage);
			}else if(instance == nextPageBtn){
				nextPageBtn.removeEventListener(MouseEvent.CLICK,nextPage);
			}else if(instance == lastPageBtn){
				lastPageBtn.removeEventListener(MouseEvent.CLICK,lastPage);
			}else if(instance == goNs){
				goNs.removeEventListener(KeyboardEvent.KEY_DOWN,keyboardPageSearch);
			}else if(instance == goBtn){
				goBtn.removeEventListener(MouseEvent.CLICK,goBtnHandler);
			}
			super.partRemoved(partName, instance);
			
		}
		/**
		 * 分发事件
		 * */
		private function dispatchSearchEvent(index:int,size:int):void{
			dispatchEvent(new PageSearchEvent(PageSearchEvent.EVENT_PAGE_SEARCH,index,size));
		}
		protected function closePua(event:FlexMouseEvent):void{
			pua.displayPopUp = false;
		}
		/**
		 * 修改每页显示数量
		 * */
		protected function keyboardSizeSearch(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.ENTER){
				dispatchSearchEvent(_page.index,NumericStepper(event.currentTarget).value);
				pua.displayPopUp = false;
			}
		}
		/**
		 * 跳转到第一页
		 * */
		protected function firstPage(e:MouseEvent):void{
			dispatchSearchEvent(1,_page.size);
			
		}
		/**
		 * 跳转到上一页
		 * */
		protected function prePage(e:MouseEvent):void{
			dispatchSearchEvent(_page.index-1,_page.size);
		}
		/**
		 * 跳转到下一页
		 * */
		protected function nextPage(e:MouseEvent):void{
			dispatchSearchEvent(_page.index+1,_page.size);
		}
		/**
		 * 跳转到最末页
		 * */
		protected function lastPage(e:MouseEvent):void{
			dispatchSearchEvent(_page.total,_page.size);
		}
		/**
		 * 根据输入页码回车跳转
		 * */
		protected function keyboardPageSearch(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.ENTER){
				dispatchSearchEvent(NumericStepper(event.currentTarget).value,_page.size);
			}
		}
		/**
		 * 根据输入 Button跳转
		 * */
		protected function goBtnHandler(e:MouseEvent):void{
			dispatchSearchEvent(goNs.value,_page.size);
		}
		
		/**
		 * 设置4个控制按钮是否可用
		 * */
		private function setControlButtonEnabled():void{
			if(_page.index <= 1){
				firstPageBtn.enabled = false;
				prePageBtn.enabled = false;
			}else{
				firstPageBtn.enabled = true;
				prePageBtn.enabled = true;
			}
			if(_page.index == _page.total){
				nextPageBtn.enabled = false;
				lastPageBtn.enabled = false;
			}else{
				nextPageBtn.enabled = true;
				lastPageBtn.enabled = true;
			}
		}
		
		
		private function createPageLabelList():void{
			removePageLabel();
			var pageStart:int = 1;
			var pageEnd:int = _page.total;
			
			if(_page.total > pageLabelSize){
				var temp:int = pageLabelSize*0.5;
				
				if(_page.index - temp >0){
					pageStart = _page.index - temp;
				}
				pageEnd = pageStart+pageLabelSize;
				if(pageEnd > _page.total){
					pageStart = pageStart-(pageEnd-_page.total);
					pageEnd = _page.total;
				}
			}
			for(pageEnd;pageEnd>=pageStart;pageEnd--){
					indexGroup.addElementAt(getPageLabel(pageEnd),0);
			}
		} 
		private function removePageLabel():void{
			try{
				pageLbls.addItem(Label(indexGroup.getElementAt(0)));
				indexGroup.removeElementAt(0);
				removePageLabel();
			}catch(e:Error){
				
			}
		}
		
		/**
		 * 获得一个pageLabel
		 * */
		private function getPageLabel(index:int):Label{
			var lbl:Label;
			if(pageLbls.length > 0){
				lbl = Label(pageLbls.getItemAt(0));
				pageLbls.removeItemAt(0);
			}else{
				lbl = new Label();
				lbl.addEventListener(MouseEvent.CLICK,pageLabelClick);
				lbl.buttonMode = true;
				lbl.useHandCursor = true;
				lbl.mouseChildren = false;
				lbl.setStyle("textDecoration","underline");
			}
			lbl.text = String(index);
			if(index == _page.index){
				lbl.setStyle("fontWeight","bold");
			}else{
				lbl.setStyle("fontWeight","normal");
			}
			return lbl;
		}
		private function pageLabelClick(event:MouseEvent):void{
			dispatchSearchEvent(parseInt(Label(event.currentTarget).text),_page.size);
		}
	}
}